<?php
namespace app\admin\controller;
use \think\Controller;
use app\admin\logic\Article as ArticleLogic;
class Article extends Base
{

	//文章列表
	public function index( $title='', $status='' )
	{
		//定义变量
		$map = array();

		//筛选条件
		if( !empty($title) ) {
			$title 			= trim( $title );
			$map ['title'] 	= 	array('like', "%{$title}%");
		}

		if( !empty($status) ) {
			$map ['status'] = $status;
		}
		
		$data  = $this->M()->dataList($map);
		$this->assign('list', $data['list']);
		$this->assign('page', $data['page']);
		return $this->fetch();
	}
	//添加文章控制器
	public function add()
	{
		//POST请求判断 
		if( $this->isPost() ) {
			//参数接收
			$post   = input();
			//逻辑层处理
			$result = $this->M()->dataAdd($post);

			//结果集处理
			if($result['bool']) {
				$this->success($this->getTipMessage('success'), url('index'));
			} else {
				$this->error($this->getTipMessage($result['code']));
			}

		} else {
			//渲染模板
			return $this->fetch('edit');
		}
	}
	//文章编辑
	public function edit($id='')
	{

		//POST请求判断
		if( $this->isPost() ) {
			//参数接收
			$post   = input();

			//逻辑层处理
			$result = $this->M()->dataUpdate( $post, $post ['id'] );
			
			//结果集处理
			if($result['bool']) {
				$this->success($this->getTipMessage('success'), url('index'));
			} else {
				$this->error($this->getTipMessage($result['code']));
			}

		} else {
			//模板赋值
			$data = $this->M()->dataFind($id);
			$this->assign( 'data', $data );
			
			//渲染模板
			return $this->fetch('edit');
		}
	}
	//文章删除
	public function del($id)
	{
		$res = $this->M()->dataDel($id);
		if($res ['bool'] == true) {
			$this->success($this->getTipMessage('success'), url('index'));
		} else {
			$this->error($this->getTipMessage($res ['code']));
		}
	}
	//多条文章删除
	public function dels($ids)
	{	
		$res = $this->M()->dataDels($ids);//dump($res);die;
		if($res ['bool']) {
			$this->success($this->getTipMessage('success'), url('index'));
		} else {
			$error = $this->getTipMessage($res ['code']) . $res ['message'];
			$this->error($error);
		}
	}

	//实例文章逻辑层
	private function M()
	{
		return new ArticleLogic();
	}


}